﻿using System;
using System.Collections.Generic;
using System.Text;
using ExtBusiness;
using ExtBusiness.Struct;
using System.Data;
using WF_DataAccess;
using WF_Business;

namespace ExtBusiness.BaseClass
{
    public class GqpzInfo
    {
       
        /// <summary>
        /// 查询人员信息 （st_user）中
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public static List<UserStruct> GetUserIdByFlowID(string username, string serialId, string flowno)
        {
            //如果查询流程ID不为空            
            string strSql = string.Format(@"select * from xt_suspendflow_userid t where t.serial_id in (select wid
                                         from st_workflow b where b.wname in (select a.wname  from st_workflow a
                                         where a.wid = '{0}'))", serialId);
            DataTable dt;
            SysParams.OAConnection().RunSql(strSql, out dt);
            DataTable dtOut = new DataTable();
            if (dt.Rows.Count > 0)
            {
                strSql = "select a.userid,c.user_name,c.login_name from xt_suspendflow_userid a,st_user c where a.userid=c.userid ";
                strSql += "and a.serial_id in (select wid from st_workflow b where b.wname in (select a.wname  from st_workflow a  where a.wid = '" + serialId + "')) and a.flow_id = (select id from xt_suspend_flow a where a.flow_no = '" + flowno + "')";
                strSql += " union all ";
                strSql += "select b.userid,b.user_name,b.login_name from st_user b where b.userid not in ";
                strSql += "(select userid from xt_suspendflow_userid where serial_id in (select wid from st_workflow b where b.wname in (select a.wname  from st_workflow a  where a.wid = '" + serialId + "')) and flow_id = (select id from xt_suspend_flow a where a.flow_no = '" + flowno + "'))";
            }
            else
            {
//                strSql = @"select b.userid, b.user_name, b.login_name  from st_user b, st_user_department a, st_department c
//                     where 1 = 1 and a.userid = b.userid and a.order_id = c.departid order by c.order_id, b.orderbyid ";
                strSql = @"select b.userid, b.user_name, b.login_name  from st_user b, st_department c
                     where b.deptid = c.departid ";
            }

            if (!string.IsNullOrEmpty(username))
            {
                strSql += " and b.user_name like '%" + username.Trim() + "%' ";
            }
            SysParams.OAConnection().RunSql(strSql, out dtOut);

            List<UserStruct> lstUserId = new List<UserStruct>();
            foreach (DataRow drTemp in dtOut.Rows)
            {
                UserStruct user = new UserStruct();
                user.UserRealName = drTemp["login_name"].ToString();
                user.UserId = drTemp["userid"].ToString();
                user.UserName = drTemp["user_name"].ToString();
                lstUserId.Add(user);
            }
            return lstUserId;
        }


        /// <summary>
        /// 已经关联的人员ID
        /// </summary>
        /// <param name="flowno"></param>
        /// <param name="iid"></param>
        /// <returns></returns>
        public static List<string> GetUserIdByFlowno(string flowID, string strFlowvalue)
        {
            string strSql = string.Format(@"select userid from xt_suspendflow_userid t where t.flow_id = '{0}' and t.serial_id  in  (select wid from st_workflow b where b.wname in (
                                    select a.wname  from st_workflow a where a.wid = '{1}'))",
                flowID, strFlowvalue);
            DataTable dtOut;
            SysParams.OAConnection().RunSql(strSql, out dtOut);

            List<string> lstUserId = new List<string>();
            foreach (DataRow drTemp in dtOut.Rows)
            {
                lstUserId.Add(drTemp["userid"].ToString());
            }
            return lstUserId;
        }


        /// <summary>
        /// 获取所有流程信息（st_workflow）
        /// </summary>
        /// <returns></returns>
        public static List<SuspendStruct> GetFlowNameInfo()
        {

            string strSql = string.Format("select t.wname,t.wid from st_workflow t where 1 = 1  and rot = 0 order by t.wname");
            DataTable dtOut;
            SysParams.OAConnection().RunSql(strSql, out dtOut);

            List<SuspendStruct> lstFlowInfo = new List<SuspendStruct>();
            foreach (DataRow drTemp in dtOut.Rows)
            {
                SuspendStruct suspend = new SuspendStruct();
                suspend.Flowname = drTemp["wname"].ToString();
                suspend.Flowno = drTemp["wid"].ToString();
                lstFlowInfo.Add(suspend);
            }
            return lstFlowInfo;
        }
       
        /// <summary>
        /// 获取（xt_suspend_flow）中ID
        /// </summary>
        /// <param name="flowno">流程号</param>
        /// <returns></returns>
        public static string GetFlowid(string flowno)
        {
            string strSql = string.Format("select t.id from xt_suspend_flow t where t.flow_no = '{0}'", flowno);
            string strFlowID = SysParams.OAConnection().GetValue(strSql);
            if (!string.IsNullOrEmpty(strFlowID))
            {
                return strFlowID;
            }
            else
                return "";
        }


    }
}
